/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package CastleDefense;

/**
 *
 * @author Administrator
 */
//import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import org.apache.commons.lang.StringUtils;

@XmlRootElement(name = "wave")
// If you want you can define the order in which the fields are written
// Optional
@XmlType(propOrder = {"id", "col_1", "col_2", "col_3", "col_4"})
public class Wave
{
    private int      id;
    private String   col_1;
    private String   col_2;
    private String   col_3;
    private String   col_4;

    // If you dont like the variable name, e.g. "name", you can easily change this
    // variable name for your XML-Output:
    public int getId()
    {
        return id;
    }

    public void setId(int id)
    {
        this.id = id;
    }

    @XmlElement(name = "col_1")
    public String getCol_1()
    {
        return col_1;
    }

    public void setCol_1(String col_1)
    {
        this.col_1 = col_1;
    }

    @XmlElement(name = "col_2")
    public String getCol_2()
    {
        return col_2;
    }

    public void setCol_2(String col_2)
    {
        this.col_2 = col_2;
    }

    @XmlElement(name = "col_3")
    public String getCol_3()
    {
        return col_3;
    }

    public void setCol_3(String col_3)
    {
        this.col_3 = col_3;
    }

    @XmlElement(name = "col_4")
    public String getCol_4()
    {
        return col_4;
    }

    public void setCol_4(String col_4)
    {
        this.col_4 = col_4;
    }

    /**
     * This method creates a 2 dimensional string array that holds 
     * the specific monster locations in each col x row for each wave
     *
     * @return A 2 dimension string array.
     */
    public String[][] getWaveArray()
    {
        int r1 = getCol_1().replaceAll("[^,]", "").length()+1;
        int r2 = getCol_2().replaceAll("[^,]", "").length()+1;
        int r3 = getCol_3().replaceAll("[^,]", "").length()+1;
        int r4 = getCol_4().replaceAll("[^,]", "").length()+1;
        int[] rowArray = {r1, r2, r3, r4};

        int m = 0;
        int r = max(rowArray);
        int c = 4;
        String[][] waveArray = new String[r][c];
        
        String[] col1 = StringUtils.splitPreserveAllTokens(getCol_1(), ',');
        String[] col2 = StringUtils.splitPreserveAllTokens(getCol_2(), ',');
        String[] col3 = StringUtils.splitPreserveAllTokens(getCol_3(), ',');
        String[] col4 = StringUtils.splitPreserveAllTokens(getCol_4(), ',');

        String[][] temp2d = {col1,col2,col3,col4};

        for (int row=0; row<waveArray.length; row++)
        {
            for (int col=0; col<waveArray[row].length; col++)
            {
                waveArray[row][col] = temp2d[col][row];
            }
        }

        return waveArray;
    }

    private int max(int[] t)
    {
        int maximum = t[0];   // start with the first value
        for (int i=1; i<t.length; i++)
        {
            if (t[i] > maximum)
            {
                maximum = t[i];   // new maximum
            }
        }
        return maximum;
    }
}

